home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #3 / Amiga Plus CD - 2002 - No. 03.iso / AmiSoft / Dev / E / Goportscan_src.lha / GoPortscan_Src / EXPERIMENTAL / ping.e < prev    next >
Encoding:
Text File  |  2003-02-02  |  5.3 KB  |  166 lines

  1. /*
  2.     Description: Experimental source on performing ICMP pings. This source is
  3.                  actually quite functional and could easily be adapted into a
  4.                  fully functional ping command.
  5.  
  6.                  By default pings localhost 30 times.
  7. */
  8.  
  9.  
  10.  
  11. MODULE  'socket',
  12.         'amitcp/sys/socket',
  13.         'amitcp/sys/types',
  14.         'amitcp/sys/time',
  15.         'amitcp/sys/param',
  16.         'amitcp/sys/errno',
  17.         'amitcp/netinet/in',
  18.         'amitcp/netinet/ip_icmp',
  19.         'amitcp/netinet/ip',
  20.         'amitcp/netdb',
  21.         'oomodules/softtimer_oo'
  22.  
  23. CONST SEQNUM=29, ICMPIDNUM=1066
  24.  
  25. OBJECT intfold
  26.     arr[100]:ARRAY OF INT
  27. ENDOBJECT
  28.  
  29. PROC main()
  30. DEF icmphdr:PTR TO icmp,
  31.     iphdr:PTR TO ip,
  32.     ricmp:PTR TO icmp,
  33.     sain:PTR TO sockaddr_in,
  34.     hst:PTR TO hostent,
  35.     rcvbuffer,
  36.     sock,
  37.     recvlen,
  38.     readfds:fd_set,
  39.     tv:compatible_timeval,
  40.     loop,
  41.     hostname[200]:STRING,
  42.     mytimer:PTR TO softtimer
  43.  
  44. IF arg[]=0
  45.     StrCopy(hostname, 'localhost')
  46. ELSE
  47.     StrCopy(hostname, arg)
  48. ENDIF
  49.  
  50. IF (socketbase:=OpenLibrary('bsdsocket.library',NIL))<>NIL
  51.     IF hst:=GetHostByName(hostname)
  52.         IF (sock:=Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP))<>-1
  53.             sain:=New(SIZEOF sockaddr)
  54.             sain.family:=AF_INET
  55.             CopyMem(Long(hst.addr_list), sain.addr, hst.length)
  56.  
  57.             NEW mytimer.softtimer()
  58.  
  59.             FOR loop:=1 TO 30
  60.  
  61.                 icmphdr:=New(SIZEOF icmp)
  62.                 iphdr:=New(SIZEOF ip)
  63.  
  64.                 ->WriteF('SENDING ICMP PACKET\n-------------------\n\n')
  65.  
  66.                 icmphdr.type:=ICMP_ECHO
  67.                 icmphdr.code:=0 ->3
  68.                 icmphdr.idseq.id:=ICMPIDNUM
  69.                 icmphdr.idseq.seq:=loop
  70.                 icmphdr.cksum:=cksum(icmphdr, SIZEOF icmp)
  71.  
  72.                 /*
  73.                 WriteF('ICMP TYPE     =  $\h (\d)\n', icmphdr.type, icmphdr.type)
  74.                 WriteF('ICMP CODE     =  $\h (\d)\n', icmphdr.code, icmphdr.code)
  75.                 WriteF('ICMP IDNUM    =  $\h (\d)\n', icmphdr.idseq.id, icmphdr.idseq.id)
  76.                 WriteF('ICMP SEQNUM   =  $\h (\d)\n', icmphdr.idseq.seq, icmphdr.idseq.seq)
  77.                 WriteF('ICMP CHECKSUM =  $\h (\d)\n', icmphdr.cksum, icmphdr.cksum)
  78.                 */
  79.  
  80.                 ->WriteF('PING!\n')
  81.  
  82.                 SendTo(sock, icmphdr, SIZEOF icmp, NIL, sain, SIZEOF sockaddr_in)
  83.  
  84.             ->ENDFOR
  85.  
  86.  
  87.             ->FOR loop:=1 TO 30
  88.  
  89.                 fd_zero(readfds)
  90.                 fd_set(sock, readfds)
  91.                 tv.sec:=0
  92.                 tv.usec:=1
  93.  
  94.                 WHILE WaitSelect(sock+1, readfds, NIL, NIL, tv, NIL)>0
  95.  
  96.                     rcvbuffer:=New((SIZEOF ip) + (SIZEOF icmp))
  97.                     recvlen:=RecvFrom(sock, rcvbuffer, (SIZEOF ip) + (SIZEOF icmp), NIL, NIL, NIL)
  98.                     ->WriteF('\n\nRECEIVED IP PACKET\n------------------\n\n')
  99.                     iphdr:=rcvbuffer
  100.                     ricmp:=rcvbuffer + (SIZEOF ip)
  101.  
  102.                     /*
  103.                     WriteF('IP VERSION (V4 is $45) = $\h (\d)\n', iphdr.v_hl, iphdr.v_hl)
  104.                     WriteF('IP SOURCE ADDRESS      = $\h (\s)\n', iphdr.src, Inet_NtoA(iphdr.src.addr))
  105.                     WriteF('IP DESTINATION ADDRESS = $\h (\s)\n', iphdr.dst, Inet_NtoA(iphdr.dst.addr))
  106.                     WriteF('IP TOS                 = $\h (\d)\n', iphdr.tos, iphdr.tos)
  107.                     WriteF('IP LENGTH (DATA PART)  = $\h (\d)\n', iphdr.len, iphdr.len)
  108.                     WriteF('IP IDNUM               = $\h (\d)\n', iphdr.id, iphdr.id)
  109.                     WriteF('IP FRAGMENT OFFSET     = $\h (\d)\n', iphdr.off, iphdr.off)
  110.                     WriteF('IP TIME TO LIVE        = $\h (\d)\n', iphdr.ttl, iphdr.ttl)
  111.                     WriteF('IP PROTOCOL (1=ICMP)   = $\h (\d)\n', iphdr.p, iphdr.p)
  112.                     WriteF('IP CHECKSUM            = $\h (\d)\n', iphdr.sum, iphdr.sum)
  113.                     WriteF('  |\n')
  114.                     WriteF('  ->\n')
  115.                     WriteF('    ICMP TYPE     =  $\h (\d)\n', ricmp.type, ricmp.type)
  116.                     WriteF('    ICMP CODE     =  $\h (\d)\n', ricmp.code, ricmp.code)
  117.                     WriteF('    ICMP IDNUM    =  $\h (\d)\n', ricmp.idseq.id, ricmp.idseq.id)
  118.                     WriteF('    ICMP SEQNUM   =  $\h (\d)\n', ricmp.idseq.seq, ricmp.idseq.seq)
  119.                     WriteF('    ICMP CHECKSUM =  $\h (\d)\n', ricmp.cksum, ricmp.cksum)
  120.                     */
  121.  
  122.                     WriteF('\d Bytes From \s: Sequence=\d: TTL=\d\n',recvlen, Inet_NtoA(iphdr.src.addr), ricmp.idseq.seq, iphdr.ttl)
  123.                 ->ELSE
  124.                 ->    WriteF('TIMED OUT\n')
  125.                 ->ENDIF
  126.  
  127.                 ENDWHILE
  128.  
  129.                 Delay(5)
  130.  
  131.             ENDFOR
  132.  
  133.         ELSE
  134.             WriteF('Problem with creating the socket')
  135.         ENDIF
  136.     ELSE
  137.         WriteF('Problem with host lookup\n')
  138.     ENDIF
  139.  
  140.     CloseSocket(sock)
  141.     CloseLibrary(socketbase)
  142. ELSE
  143.     WriteF('Unable to open bsdsocket.library\n')
  144. ENDIF
  145.  
  146.  
  147. ENDPROC
  148.  
  149. PROC cksum(hdr:PTR TO intfold, hdrsize:LONG)
  150. DEF accumulator=0:LONG,
  151.     loop
  152.  
  153. ->WriteF('Using Headersize $\h (\d)\n',hdrsize, hdrsize)
  154.  
  155. FOR loop:=0 TO ((hdrsize-1)/2)
  156.     accumulator:=accumulator+hdr.arr[loop]
  157. ENDFOR
  158.  
  159. accumulator:=(Shr(accumulator, 16)) + (Eor(accumulator, $FFFF))
  160. accumulator:=accumulator + (Shr(accumulator, 16))
  161.  
  162. ->WriteF('Calculated Checksum = $\h (\d)\n',accumulator, accumulator)
  163.  
  164. ENDPROC accumulator
  165.  
  166.